package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.education.common.Result;
import com.yqg.education.entity.Exampaper;
import com.yqg.education.entity.Examquestion;
import com.yqg.education.entity.Question;
import com.yqg.education.mapper.ExampaperMapper;
import com.yqg.education.service.IExampaperService;
import com.yqg.education.service.IExamquestionService;
import com.yqg.education.service.IQuestionService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * ExampaperController 前端控制器
 *
 * @author zxf
 * @since 2025-04-16
 */
@RestController
@RequestMapping("/exampaper")
public class ExampaperController {


    @Resource
    private IExampaperService exampaperService;

    @Resource
    private ExampaperMapper exampaperMapper;

    @Resource
    private IQuestionService questionService;

    @Resource
    private IExamquestionService examquestionService;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    public Result save(@RequestBody Exampaper exampaper) {
        try {
            exampaperService.savePaper(exampaper);
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
        return Result.success();
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        Exampaper exampaper = exampaperService.getById(id);
        exampaper.setIsDelete(1);
        return Result.success(exampaperService.saveOrUpdate(exampaper));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Exampaper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<Exampaper> list = exampaperService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(exampaperService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String username,
                           @RequestParam(required = false) Integer trainClassId,
                           @RequestParam(required = false) Integer courseId,
                           @RequestParam(required = false) Integer isPass,
                           @RequestParam(required = false) Integer stuId) {
        pageNum = pageNum * pageSize - pageSize;
        List<Exampaper> exampaperList = exampaperMapper.findPage(pageNum, pageSize, username, trainClassId, courseId, isPass, stuId);
        Long count = exampaperMapper.findCount(username, trainClassId, courseId, isPass, stuId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", exampaperList);
        map.put("total", count);
        return Result.success(map);
    }
    @GetMapping("/pageAdmin")
    public Result findPageAdmin(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String courseName) {
        pageNum = pageNum * pageSize - pageSize;
        List<Exampaper> exampaperList = exampaperMapper.findPageAdmin(pageNum, pageSize,courseName );
        Long count = exampaperMapper.findCountAdmin(courseName);
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", exampaperList);
        map.put("total", count);
        return Result.success(map);
    }

    /**
     * 学员端考试页面接口实现
     */
    @GetMapping("/stufindExampaperPage")
    public Result stufindExampaperPage(@RequestParam Long userId,
                                       @RequestParam Long courseId){
        return Result.success(exampaperService.stuFindExampaperPage(userId,courseId));

    }
    /**
     * 考试正确答案页面接口实现
     */
    @GetMapping("/findTrueResultPage")
    public Result findTrueResultPage(@RequestParam Long userId,
                                        @RequestParam Long exampaperId){
        return Result.success(exampaperService.findTrueResultPage(userId,exampaperId));
    }
    /**
     * 学员端考试正确答案页面接口实现
     */
    @GetMapping("/stuFindTrueResultPage")
    public Result stuFindTrueResultPage(@RequestParam Long userId,
                                       @RequestParam Long courseId){
        return Result.success(exampaperService.stuFindTrueResultPage(userId,courseId));
    }
    /**
     * 后端查看试卷详情
     */
    @GetMapping("/findQuestionByExampaperId")
    public Result findQuestionByExampaperId(@RequestParam Long exampaperId){
        List<Question> questionList = exampaperMapper.findQuestionByExampaperId(exampaperId);
        Long count = (long)questionList.size();
        HashMap<String, Object> map = new HashMap<>();
        map.put("records", questionList);
        map.put("total", count);
        return Result.success(map);
    }
}

